package com.chris.libs.devtools.x.v1.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * create by Chris Chan
 * create on 2019/7/9 22:19
 * use for :
 */
public class GlideUtil extends UtilsManager {
    private static String imageUrlPrefix = "";

    public static void setImageUrlPrefix(String prefix) {
        GlideUtil.imageUrlPrefix = prefix;
    }

    //普通系列
    public static void load(Context context, @RawRes @DrawableRes int imgId, ImageView imageView) {
        Glide.with(context).load(imgId).into(imageView);
    }

    public static void load(Context context, Uri uri, ImageView imageView) {
        Glide.with(context).load(uri).into(imageView);
    }

    public static void load(Context context, String imgUrl, ImageView imageView) {
        Glide.with(context).load(imgUrl).into(imageView);
    }

    public static void loadUrl(Context context, String imgUrl, ImageView imageView) {
        load(context, GlideUtil.imageUrlPrefix + imgUrl, imageView);
    }

    public static void load(Context context, Bitmap bitmap, ImageView imageView) {
        Glide.with(context).load(bitmap).into(imageView);
    }

    public static void load(Context context, Drawable drawable, ImageView imageView) {
        Glide.with(context).load(drawable).into(imageView);
    }

    //适配屏幕宽度系列
    public static class MatchScreenWidth {

        private static CustomTarget<Drawable> createCustomTarget(ImageView imageView) {
            return new CustomTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    match(resource, imageView);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            };
        }

        /**
         * 适配屏幕宽度
         *
         * @param resource
         * @param imageView
         */
        private static void match(@NonNull Drawable resource, ImageView imageView) {
            //适配屏幕宽度
            int width = resource.getIntrinsicWidth();
            int height = resource.getIntrinsicHeight();

            int screenWidth = ScreenUtil.getWidth();
            int newHeight = screenWidth * height / width;

            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = screenWidth;
            layoutParams.height = newHeight;
            imageView.setLayoutParams(layoutParams);
            imageView.setImageDrawable(resource);
        }

        public static void load(Context context, @RawRes @DrawableRes int imgId, ImageView imageView) {
            Glide.with(context).asDrawable().load(imgId).into(createCustomTarget(imageView));
        }

        public static void load(Context context, Uri uri, ImageView imageView) {
            Glide.with(context).asDrawable().load(uri).into(createCustomTarget(imageView));
        }

        public static void load(Context context, String imgUrl, ImageView imageView) {
            Glide.with(context).asDrawable().load(imgUrl).into(createCustomTarget(imageView));
        }

        public static void loadUrl(Context context, String imgUrl, ImageView imageView) {
            load(context, GlideUtil.imageUrlPrefix + imgUrl, imageView);
        }

        public static void load(Context context, Bitmap bitmap, ImageView imageView) {
            Glide.with(context).asDrawable().load(bitmap).into(createCustomTarget(imageView));
        }

        public static void load(Context context, Drawable drawable, ImageView imageView) {
            Glide.with(context).asDrawable().load(drawable).into(createCustomTarget(imageView));
        }
    }

    //适配给定宽高系列
    public static class MatchMaxXY {

        public static CustomTarget<Drawable> createCustomTarget(ImageView imageView, int maxWidth, int maxHeight) {
            return new CustomTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    match(resource, imageView, maxWidth, maxHeight);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            };
        }

        /**
         * 适配给定宽高
         * 在保持比例的情况下适配给定的宽高
         * ? 小于等0不适配
         *
         * @param resource
         * @param imageView
         * @param maxWidth
         * @param maxHeight
         */
        private static void match(@NonNull Drawable resource, ImageView imageView, int maxWidth, int maxHeight) {
            //适配屏幕宽度
            int width = resource.getIntrinsicWidth();
            int height = resource.getIntrinsicHeight();

            //图像宽高比
            int rImage = width / height;
            //最大宽高比
            int rMax = maxWidth / maxHeight;

            int nWidth, nHeight;

            //如果图像宽高比大于最高宽高比，则适配高度，反之适配宽度
            if (rImage > rMax) {
                nWidth = maxHeight * width / height;
                nHeight = maxHeight;
            } else {
                nWidth = maxWidth;
                nHeight = maxWidth * height / width;
            }

            //重新设置布局参数
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = nWidth;
            layoutParams.height = nHeight;
            imageView.setLayoutParams(layoutParams);
            imageView.setImageDrawable(resource);
        }

        public static void load(Context context, @RawRes @DrawableRes int imgId, ImageView imageView, int maxWidth, int maxHeight) {
            Glide.with(context).asDrawable().load(imgId).into(createCustomTarget(imageView, maxWidth, maxHeight));
        }

        public static void load(Context context, Uri uri, ImageView imageView, int maxWidth, int maxHeight) {
            Glide.with(context).asDrawable().load(uri).into(createCustomTarget(imageView, maxWidth, maxHeight));
        }

        public static void load(Context context, String imgUrl, ImageView imageView, int maxWidth, int maxHeight) {
            Glide.with(context).asDrawable().load(imgUrl).into(createCustomTarget(imageView, maxWidth, maxHeight));
        }

        public static void loadUrl(Context context, String imgUrl, ImageView imageView, int maxWidth, int maxHeight) {
            load(context, GlideUtil.imageUrlPrefix + imgUrl, imageView, maxWidth, maxHeight);
        }

        public static void load(Context context, Bitmap bitmap, ImageView imageView, int maxWidth, int maxHeight) {
            Glide.with(context).asDrawable().load(bitmap).into(createCustomTarget(imageView, maxWidth, maxHeight));
        }

        public static void load(Context context, Drawable drawable, ImageView imageView, int maxWidth, int maxHeight) {
            Glide.with(context).asDrawable().load(drawable).into(createCustomTarget(imageView, maxWidth, maxHeight));
        }
    }

    /**
     * 适配最终的宽
     * 高按照比例确定
     */
    public static class MatchWidth {

        public static CustomTarget<Drawable> createCustomTarget(ImageView imageView, float ratio) {
            return new CustomTarget<Drawable>() {
                @Override
                public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                    match(resource, imageView, ratio);
                }

                @Override
                public void onLoadCleared(@Nullable Drawable placeholder) {

                }
            };
        }

        /**
         * 适配给最终宽度
         *
         * @param resource
         * @param imageView
         * @param ratio
         */
        private static void match(@NonNull Drawable resource, ImageView imageView, float ratio) {
            int width = imageView.getMeasuredWidth();

            //重新设置布局参数
            ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
            layoutParams.width = width;
            layoutParams.height = (int) (width * ratio);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageDrawable(resource);
        }

        public static void load(Context context, @RawRes @DrawableRes int imgId, ImageView imageView, float ratio) {
            Glide.with(context).asDrawable().load(imgId).into(createCustomTarget(imageView, ratio));
        }

        public static void load(Context context, Uri uri, ImageView imageView, float ratio) {
            Glide.with(context).asDrawable().load(uri).into(createCustomTarget(imageView, ratio));
        }

        public static void load(Context context, String imgUrl, ImageView imageView, float ratio) {
            Glide.with(context).asDrawable().load(imgUrl).into(createCustomTarget(imageView, ratio));
        }

        public static void loadUrl(Context context, String imgUrl, ImageView imageView, float ratio) {
            load(context, GlideUtil.imageUrlPrefix + imgUrl, imageView, ratio);
        }

        public static void load(Context context, Bitmap bitmap, ImageView imageView, float ratio) {
            Glide.with(context).asDrawable().load(bitmap).into(createCustomTarget(imageView, ratio));
        }

        public static void load(Context context, Drawable drawable, ImageView imageView, float ratio) {
            Glide.with(context).asDrawable().load(drawable).into(createCustomTarget(imageView, ratio));
        }
    }

}
